﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace PPI.General
{
    public class DirHelper
    {
        /// <summary>
        /// return full sdf path
        /// </summary>
        /// <param name="Folder"></param>
        /// <returns></returns>
        public static string GetSdfPath(string Folder)
        {
            try
            {
                DirectoryInfo _d = new DirectoryInfo(Folder);
                FileInfo[] _Files = _d.GetFiles("*.sdf");
                if (_Files.Length > 0)
                    return _Files[0].FullName;
                return "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Copy files and folders  in  recursive action
        /// </summary>
        /// <param name="diSource">Source Directory</param>
        /// <param name="diDestination">Destination Directory</param>
        /// <param name="blOverwrite">overwrite: True, False</param>
        public static void CopyFiles(DirectoryInfo diSource, DirectoryInfo diDestination, bool blOverwrite)
        {
            DirectoryInfo[] diSourceSubDirectories;
            FileInfo[] fiSourceFiles;

            //obtengo todos los archivos del directorio origen
            fiSourceFiles = diSource.GetFiles();
            //obtengo los subdirectorios (si existen)
            diSourceSubDirectories = diSource.GetDirectories();

            //si no existe el directorio destino crearlo
            if (!diDestination.Exists) diDestination.Create();

            //Usar la recursividad para navegar por los subdirectorios
            //e ir obteniendo los archivos hasta llegar al final
            foreach (DirectoryInfo diSourceSubDirectory in diSourceSubDirectories)
            {
                CopyFiles(diSourceSubDirectory, new DirectoryInfo(diDestination.FullName + "\\" +
                  diSourceSubDirectory.Name), blOverwrite);
            }

            foreach (FileInfo fiSourceFile in fiSourceFiles)
            {
                if (fiSourceFile.Extension != "xml")
                {
                    fiSourceFile.CopyTo(diDestination.FullName + "\\" + fiSourceFile.Name, blOverwrite);
                }
            }
        }

        /// <summary>
        /// Get the full path of images directory from Mobile connection string.
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static string GetImagesDirectory(string connectionString)
        {
            string returnPath = "";
            if (!string.IsNullOrEmpty(connectionString))
            {
                DirectoryInfo _ConnectionDirectory = new DirectoryInfo(connectionString);
                DirectoryInfo[] _ImagesDirectory = _ConnectionDirectory.Parent.GetDirectories("Images");
                if (_ImagesDirectory.Length > 0)
                    returnPath = _ImagesDirectory[0].FullName;
            }
            return returnPath;

        }

        /// <summary>
        /// Get the the server folder contains the images.
        /// </summary>
        /// <param name="assetId"></param>
        /// <param name="imageId"></param>
        /// <returns></returns>
        public static string GetImageDirectory(int assetId, int imageId)
        {
            int Start = 0;
            int End = 99;
            string _Directory = "";
            bool _FoundPosition = false;
            while (!_FoundPosition)
            {
                if (assetId > Start && assetId > End)
                {
                    Start += 100;
                    End += 100;
                }
                else
                {
                    _FoundPosition = true;
                    _Directory = Start.ToString() + "-" + End.ToString();
                }
            }
            return _Directory;
        }

        /// <summary>
        /// Copy a image file to corresponding folder in Outbox Directory.
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="fullSourcePath"></param>
        /// <param name="destinationName"></param>
        /// <returns></returns>
        public static string CopyImageToOutbox(string connectionString, string fullSourcePath, string destinationName)
        {
            try
            {
                DirectoryInfo destinationDirectory = new DirectoryInfo(GetImagesDirectory(connectionString));
                FileInfo source = new FileInfo(fullSourcePath);

                string returnPath = source.FullName;

                if (source.Exists && destinationDirectory.Exists)
                {
                    FileInfo destination = new FileInfo(destinationDirectory.FullName + "//" + destinationName);
                    source.CopyTo(destination.FullName, true);
                    returnPath = destination.FullName;
                }
                return returnPath;
            }
            catch(Exception ex)
            {
                throw  ex;
            }

        }
    }
}
